package utils

import (
	"sync"
	"sync/atomic"
)

// RoundRobinProxySwitcher 定义轮询代理切换器
type RoundRobinProxySwitcher struct {
	mu      sync.Mutex
	index   int64
	proxies []string
}

func NewRoundRobinProxySwitcher(proxies []string) *RoundRobinProxySwitcher {
	return &RoundRobinProxySwitcher{
		index:   0,
		proxies: proxies,
	}
}

func (r *RoundRobinProxySwitcher) GetProxy() string {
	atomic.AddInt64(&r.index, 1)
	value := atomic.LoadInt64(&r.index)
	if value > 1000000 {
		// 超过100w设置为0
		atomic.StoreInt64(&r.index, 0)
	}
	return r.proxies[int(value)%(len(r.proxies))]
}
